package com.ar.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ar.studyapp.R;

/**
 * The Class SubjectAdapter.
 * @author anh
 */
public class SubjectAdapter extends BaseAdapter {
	
	
	private Context context;
	
	/** The subject list. */
	private String subjectList;
	
	private LayoutInflater inflater;
	

	/**
	 * Instantiates a new subject adapter object.
	 *
	 * @param context the context
	 * @param subjectList the subject list
	 */
	public SubjectAdapter(Context context, String subjectList) {
		this.context = context;
		this.subjectList = subjectList;
		this.inflater = LayoutInflater.from(context);
	}
	
	/**
	 * Gets the subject list.
	 *
	 * @param subjectList the subject list
	 * @return the subject list
	 */
	private JSONArray getSubjectList(String subjectList){
		try {
			return new JSONArray(subjectList);
		} catch (JSONException e) {
			return new JSONArray();
		}
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		
		return getSubjectList(subjectList).length();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int arg0) {
		
		return null;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int arg0) {
		
		return 0;
	}
	
	public String getCategoryId(int rowIndex) {
		try {
			return getSubjectList(subjectList).getJSONObject(rowIndex).getString("id");
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	public String getSubjectName(int rowIndex) {
		try {
			return getSubjectList(subjectList).getJSONObject(rowIndex).getString("name");
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		
		ViewHolder holder = new ViewHolder();
		if (convertView==null){
			convertView = inflater.inflate(R.layout.object_row, null);
			holder.imgViewSubjectIcon = (ImageView) convertView.findViewById(R.id.imgView_subjIcon);
			holder.tvSubjectName = (TextView) convertView.findViewById(R.id.tv_subjName);
			convertView.setTag(holder);
			}
		else holder = (ViewHolder)convertView.getTag();
		try {
			String background = getSubjectList(subjectList).getJSONObject(pos).getString("background");
			String subjectName = getSubjectList(subjectList).getJSONObject(pos).getString("name");
			String imgName = getSubjectList(subjectList).getJSONObject(pos).getString("image");
			convertView.setBackgroundColor(Color.parseColor(background));
			holder.imgViewSubjectIcon.setImageDrawable(context.getResources().getDrawable(context.getResources().getIdentifier(imgName, "drawable", context.getPackageName())));
			holder.tvSubjectName.setTypeface(Typeface.createFromAsset(context.getAssets(), "segoescb.ttf"));
			holder.tvSubjectName.setText(subjectName);
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		
		return convertView;
	}
	
	class ViewHolder{
		ImageView imgViewSubjectIcon;
		TextView tvSubjectName;
	}

}
